<?php
/**
 * Html工具类
 */

namespace backend\utils;


use yii\helpers\BaseHtml;
use yii\helpers\Url;

class XHtml extends BaseHtml
{
    /**
     * 新建
     * @param string $text
     * @param null $url
     * @param array $options
     * @return string
     */
    public static function create($text = '', $url = null, $options = [])
    {
        $icon = '<i class="fa fa-hand-o-right"></i>';
        $text = $icon . ' 新建' . $text;
        if ($url !== null) {
            $options['href'] = Url::to($url);
        } else {
            $options['href'] = Url::to(['create']);
        }
        if (!isset($options['class'])) {
            $options['class'] = 'btn btn-primary btn-flat btn-xs';
        }
        return static::tag('a', $text, $options);
    }

    /**
     * 提交
     * @param string $content
     * @param array $options
     * @return string
     */
    public static function submitButton($content = '', $options = [])
    {
        $icon = '<i class="fa fa-check"></i>';
        if ($content == '') {
            $content = '保存';
        }
        $content = $icon . $content;
        $options['type'] = 'submit';
        if (!isset($options['class'])) {
            $options['class'] = 'btn btn-primary';
        }
        return static::button($content, $options);
    }

    /**
     * 后退
     * @param string $content
     * @param array $options
     * @return string
     */
    public static function backButton($content = '', $options = [])
    {
        if ($content == '') {
            $content = '<i class="fa fa-backward"></i>返回';
        }
        if (!isset($options['type'])) {
            $options['type'] = 'button';
        }
        if (!isset($options['class'])) {
            $options['class'] = 'btn btn-info';
        }
        if (!isset($options['onclick'])) {
            $options['onclick'] = 'javascript:history.go(-1);';
        }
        return static::tag('button', $content, $options);
    }

    /**
     * 重置
     * @param string $content
     * @param array $options
     * @return string
     */
    public static function resetButton($content = '', $options = [])
    {
        if ($content == '') {
            $content = '<i class="fa fa-retweet"></i>重置';
        }
        if (!isset($options['class'])) {
            $options['class'] = 'btn btn-warning';
        }
        $options['type'] = 'reset';
        return static::button($content, $options);
    }

    /**
     * @param string $name
     * @param string $value
     * @param string $type
     * @param array $items
     * @param array $options
     * @return string
     */
    public static function createByConfig($name, $value, $type = 'input', $items = [], $options = ['class' => 'form-control'])
    {
        $tag = '';
        switch ($type) {
            case 'textarea':
                $tag = static::textarea($name, $value, $options);
                break;
            case 'select':
                $tag = static::dropDownList($name, $value, $items, $options);
                break;
            case 'file':
                $tag = static::fileInput($name, $value, $options);
                break;
            case 'checkbox':
                $tag = static::checkboxList($name, $value, $items, $options);
                break;
            case 'radio':
                $tag = static::radioList($name, $value, $items, $options);
                break;
            default:
                $tag = static::textInput($name, $value, $options);
                break;
        }
        return $tag;
    }
}